package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;

/**
 * 添加设备
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class AddDeviceDemo extends BackHandledFragment {

	private View rootView;
	private EditText et_devicename,et_deviceimei;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_adddevicedemo, null);
		initView();
		return rootView;
	}

	private void initView() {
		// TODO Auto-generated method stub
			((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.add_device_title2);
		rootView.findViewById(R.id.tv_next).setOnClickListener(listener);
		et_devicename = ((EditText) rootView.findViewById(R.id.et_devicename));
		et_deviceimei = ((EditText) rootView.findViewById(R.id.et_deviceimei));
	}

	/**
	 * New instance edit device message.
	 */
	public static AddDeviceDemo newInstance() {
		AddDeviceDemo fragment = new AddDeviceDemo();
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.tv_next:
						ApiUtil.getApiService().scanDevice(DemoApplication.getToken(), ((EditText) rootView.findViewById(R.id.et_devicename)).getText().toString(),"1",((EditText) rootView.findViewById(R.id.et_deviceimei)).getText().toString()).enqueue(new Callback<MessageForSimple>() {
							@Override
							public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
								try {
									MessageForSimple entity = response.body();
									if(entity.getCode().equals("10008")){
										getActivity().finish();
									}else{
										Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							@Override
							public void onFailure(Call<MessageForSimple> call, Throwable t) {
								Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
							}
						});

					break;
				default:
					break;
			}
		}
	};

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
